package sdk.webview.fmc.com.fmcsdk.util;


import android.content.Context;
import android.text.TextUtils;

import java.util.List;

import sdk.webview.fmc.com.fmcsdk.bean.SysSettingsInfoBean;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

import sdk.webview.fmc.com.fmcsdk.bean.SysKesDescBean;
import sdk.webview.fmc.com.fmcsdk.bean.User;

/**
 * Created by Shark on 2016/9/28.
 */

public class UserHelper {
    public static void saveSysSettingsInfoBean(SysSettingsInfoBean settingsInfoBean) {
        PreferencesHelper.saveData(settingsInfoBean);
    }

    public static SysSettingsInfoBean getSysSettingsInfoBean() {
        SysSettingsInfoBean settingsInfoBean = PreferencesHelper.getData(SysSettingsInfoBean.class);
        return settingsInfoBean;
    }


    public static void saveSysKeyDescBean(SysKesDescBean settingsInfoBean) {
        PreferencesHelper.saveData(settingsInfoBean);
    }

    public static SysKesDescBean getSysKeyDescBean() {
        SysKesDescBean settingsInfoBean = PreferencesHelper.getData(SysKesDescBean.class);
        return settingsInfoBean;
    }

    public static void saveUser(User user) {
        PreferencesHelper.saveData(user);
    }

    public static User getSavedUser() {
        User user = PreferencesHelper.getData(User.class);
        return user;
    }

    public static String getToken() {
        User user = getSavedUser();
        if (user != null) {

            try {
                String tokenStr = DesUtil.decode(user.getRecord().getAccessToken());
                return tokenStr;
            } catch (Exception e) {
                e.printStackTrace();
                return user.getRecord().getAccessToken();
            }
        } else {
            return "";
        }
    }



    public static String getUserName() {
        User user = getSavedUser();
        if (user != null)
            return user.getRecord().getUserName();
        else
            return "";
    }

    public static String getUserPhone() {
        User user = getSavedUser();
        if (user != null)
            return user.getRecord().getPhone();
        else
            return "";
    }

    public static String getUserId() {
        User user = getSavedUser();
        if (user != null)
            return user.getRecord().getUserId();
        else
            return "";
    }

    public static String getUserOwnerOrgId() {
        User user = getSavedUser();
        if (user != null)
            return user.getRecord().getOwnerOrgId();
        else
            return "";
    }

    //清除用户信息
    public static void clearUserInfo() {
        PreferencesHelper.remove(User.class);
    }


    /**
     * 根据不同的code 获取值
     *
     * @param code
     * @return
     */
    public static String getSysSettingsValueBycode(String code) {
        String value = "";
        SysSettingsInfoBean sysSettingsInfoBean = getSysSettingsInfoBean();
        if (sysSettingsInfoBean == null) {
            return "";
        }
        List<SysSettingsInfoBean.SettingInfo> records = sysSettingsInfoBean.getRecords();
        if (records == null) {
            return "";
        }

        for (SysSettingsInfoBean.SettingInfo settingInfo : records) {
            if (settingInfo.getCode().equals(code)) {
                value = settingInfo.getValue();
                break;
            }
        }
        if ("PWD_INIT".equals(code)) {  //用户初始密码
            if (TextUtils.isEmpty(value)) {
                value = "123456";
            }
        }
        records = null;
        return value;
    }


    /**
     * 根据不同的code 获取值
     * <p>
     * type='OFFLINE_FILE_DIVIDE'
     * 文件除数：code='FILE_DIVIDE_NUM' 每组上传的最大数目
     * 总组数：code='FILE_DIVIDE_GROUPS'
     * 离线心跳间隔上限：code='OFFLINE_HEART_INTERVAL'
     *
     * @param code
     * @return
     */
    public static String getSysKeyDescValueBycode(String code) {
        String value = "";
        SysKesDescBean sysSettingsInfoBean = getSysKeyDescBean();
        if (sysSettingsInfoBean == null) {
            if ("FILE_DIVIDE_NUM".equals(code)) {  //文件除数
                value = "10";
            } else if ("FILE_DIVIDE_GROUPS".equals(code)) {  //总组数
                value = "20";
            } else if ("OFFLINE_HEART_INTERVAL".equals(code)) {  //离线心跳间隔上限
                value = "36000";
            }
            return value;
        }
        List<SysKesDescBean.SysDescInfo> records = sysSettingsInfoBean.getRecords();
        if (records == null) {
            return "";
        }
        for (SysKesDescBean.SysDescInfo sysDescInfo : records) {
            if (sysDescInfo.getCode().equals(code)) {
                value = sysDescInfo.getSpecialLabel();
                break;
            }
        }
        if ("PWD_INIT".equals(code)) {  //用户初始密码
            if (TextUtils.isEmpty(value)) {
                value = "123456";
            }
        } else if ("FILE_DIVIDE_NUM".equals(code)) {  //文件除数
            if (TextUtils.isEmpty(value)) {
                value = "10";
            }
        } else if ("FILE_DIVIDE_GROUPS".equals(code)) {  //总组数
            if (TextUtils.isEmpty(value)) {
                value = "20";
            }
        } else if ("OFFLINE_HEART_INTERVAL".equals(code)) {  //离线心跳间隔上限
            if (TextUtils.isEmpty(value)) {
                value = "36000";
            }
        }
        Log.e("shuchang", "字典---" + code + "--->" + value);
        records = null;
        return value;
    }
}
